<?php 
/***
    Amrita ITEWS - Copyright (C) 2009 Amrita Vishwa Vidyapeetham, Amritapuri.
                                     (http://www.amrita.edu)
    ***************************************************************************
    This file is part of the Amrita ITEWS distribution.
    Amrita ITEWS is free software; you can redistribute it and/or modify it 
    under the terms of the GNU General Public License (version 2) as published 
    by the Free Software Foundation AND MODIFIED BY the Amrita ITEWS exception.
    ***NOTE*** The exception to the GPL is included to allow you to distribute
    a combined work that includes Amrita ITEWS without being obliged to provide
    the source code for proprietary components outside of the Amrita ITEWS
    software. Amrita ITEWS is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    Public License for more details. You should have received a copy of the GNU
    General Public License and the Amrita ITEWS license exception along with 
    Amrita ITEWS if not then it can be viewed here: 
    http://itews.amrita.ac.in/license.html.
    
    Documentation, latest information, license and contact details are at:
    http://itews.amrita.ac.in/
 
    Amrita ITEWS source code can be found at:
    http://code.google.com/p/itews"; 
    
    The initial code-base of Amrita ITEWS was developed by Amrita Vishwa 
    Vidyapeetham as part of the project titled,\"Intelligent & Interactive 
    Telematics using Emerging Wireless Technologies for Transport Systems 
    (ITEWS)\" sponsored by Technology Information Forecasting and Assessment 
    Council (TIFAC), India.
***/
?>﻿<?php
// Setting the Content-Type header with charset
header('Content-Type: text/html; charset=utf-8');
session_start();
	$_SESSION['currentContext']="downloadStatus";
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
body {
    font: Verdana,Arial,sans-serif;
	 /* An explicit background color is required for Safari. */
	 /* Otherwise your corner chunks will come out black!    */
	background: #000055;
	Overflow:hidden
}
div.main {width:100%;text-align:center;overflow:auto}
div.section { clear: left; }
h1 { font-size: 150%; padding: 0 }
h2 { background: #ccc; padding: 1px 20px;  }
div.demoInitial { 
	float: left; width: 35em; padding: 5px; margin: 1em; font-size:12pt; font-weight:bold;
    background: #005; color:#FFF; text-align: center; font-family: verdana, arial, sans-serif;
}
div.demoStatus { 
	float: left; width: 49em; padding: 5px; margin: 1em; font-size:9pt; font-weight:bold;
    background: #FFF; color:#000; text-align: center; font-family: verdana, arial, sans-serif;
}
div.demoSuccess { 
	float: left; width: 10em; padding: 5px; margin: 1em; font-size:12pt; font-weight:bold;
    background: #0F0; color:#000; text-align: center; font-family: verdana, arial, sans-serif;
}
div.demoWarning2 { 
	float: left; width: 10em; padding: 5px; margin: 1em; font-size:12pt; font-weight:bold;
    background: #000; color:#FFF; text-align: center; font-family: verdana, arial, sans-serif;
}
div.demoWarning1 { 
	float: left; width: 10em; padding: 5px; margin: 1em; font-size:12pt; font-weight:bold;
    background: #A00; color:#FFF; text-align: center; font-family: verdana, arial, sans-serif;
}

div.fun  { margin: 2px; }
a {text-decoration:none;}
</style>
<!--link rel="stylesheet" href="thickbox.css" type="text/css" media="screen" /-->
<script type="text/javascript" src="jquery-1.2.3.js"></script>
<!--script type="text/javascript" src="thickbox.js"></script-->
<script type="text/javascript" src="jquery.corner.js"></script>
<script type="text/javascript" src="jqueryprogressbar.js""></script>
<script type="text/javascript" src="jquery.timer.js"></script>
	<script type="text/javascript">
	   //$(document).ready(
		//$($.everyTime(3000,function(){
		//$($.timer(3000, function (timer) {

		$(function() {
			//alert("***");
			$('#progressbar').css('display','none');
			$('div.demoStatus').css('display','none');
			$('div.demoSuccess').css('display','none');
			$('div.demoWarning1').css('display','none');
			$('div.demoWarning2').css('display','none');
			
			var counter = 0;
			
			$.timer(3000, function (timer) {
			if(counter==0){
				$('div.demoInitial').css('display','none');
				$('div.demoStatus').css('display','block');
				$('div.demoSuccess').css('display','block');
				$('div.demoWarning1').css('display','block');
				$('div.demoWarning2').css('display','block');
			}
			
				$.ajax({
				    url: 'downloadstatus_xml.php',
				    type: 'GET',
				    dataType: 'xml',
				    timeout: 10000,
				    error: function(){
				        //alert('Error loading XML document'); EAT IT
				    },
				    success: function(xml){
				        //$(xml).find('NearbyPlaces').find('place').each(function(){
						$(xml).find('CurrentDownloadPercentage').each(function(){
				        var item_text = $(this).text();
						$("#progressbar").reportprogress(item_text);
						});
						
						$(xml).find('TotalToBeDownloaded').each(function(){
				        var item_text = $(this).text();
						$("#total").html(item_text);
						});					

						$(xml).find('AlreadyDownloaded').each(function(){
				        var item_text = $(this).text();
						$("#completed").html(item_text);
						});	

						$(xml).find('NotFound').each(function(){
				        var item_text = $(this).text();
						$("#notfound").html(item_text);
						});	

						$(xml).find('NotYetKnown').each(function(){
				        var item_text = $(this).text();
						$("#unknown").html(item_text);
						});	
/*
if in hotzone and downloading, show current %age and relative url

if in hotzone and scanning, then show scanning message and hide current %age

if in hotzone and all done, show message - All downloads complete

out of hotzone, either show "downloads paused" or "All downloads complete
*/
var in_media_hotzone;
var download_url;
var data_rate;
						$(xml).find('InMediaHotzone').each(function(){
							in_media_hotzone = $(this).text();
						});	

						$(xml).find('StatusCode').each(function(){
				        var item_text = $(this).text();
						if(in_media_hotzone=="T"){
							if(item_text=="1"){//Downloading
								$('#progressbar').css('display','block');
								$(xml).find('CurrentDataRate').each(function(){
									data_rate = $(this).text();
								});
								$(xml).find('CurrentDownloadURL').each(function(){
									var item_text = $(this).text();
									$("#status").html(" Downloading <span style=\"border:1px solid #005;color:black\">&nbsp;" + item_text + "&nbsp;</span><P>Data Rate <span style=\"border:1px solid #005;color:black\">&nbsp;" + data_rate + "&nbsp;</span>");
								});
								
							}
							else if(item_text=="-1"){//Resolving error
								$('#progressbar').css('display','none');
								$("#status").html('Resolving Host Failed ' +' <img src="images/resolving_error.png"/>');
							}
							else if(item_text=="-2"){//Connection error
								$('#progressbar').css('display','none');
								$("#status").html('Connection Failed '+' <img src="images/connection_error.png"/>');
							}
							else if(item_text=="2"){
								$('#progressbar').css('display','none');
								$("#status").html('Scanning for download ' + ' <img src="images/spinner.gif"/>');
							}
							else if(item_text=="10"){
								$('#progressbar').css('display','none');
								$("#status").html("Download Complete");
							}
							else if(item_text=="99"){
								$('#progressbar').css('display','none');
								$("#status").html("Unknown Status!");
							}
						}
						else{
							if(item_text=="10"){
								$('#progressbar').css('display','none');
								$("#status").html("Download Complete");
							}
							else if(item_text=="11"){
								$('#progressbar').css('display','none');
								$("#status").html("Download Paused...");
							}
						}
						});	
						
					}	
				});
				if (counter>100)//just to avoid int overflow after running continuously for long time
					counter=9;
				else
					counter++;
			});
        $('div.demoStatus').each(function() {
			$(this).corner("12px"); 
	        });

			$('div.demoSuccess').each(function() {
			// The text of the paragraphs in the rounded divs is also the
			// jQuery code needed to create that effect. Cosmic.
             //eval($('p', this).text());
		$(this).corner("15px"); 
	        });

        $('div.demoWarning1').each(function() {
			$(this).corner("15px"); 
	        });

        $('div.demoWarning2').each(function() {
			$(this).corner("15px"); 
	        });

		//$("#run").css('display','none');
		//$("#reset").css('display','none');
	    $('#main p').wrap("<code></code>");
		//$("#progressbar").reportprogress(38.5);

	 
		});
  	</script>
  <style type="text/css">
	/* progress bar container */
	#progressbar{
		border:1px solid white;
		width:620px;
		height:75px;
		position:relative;
		color:black; 
	}
	/* color bar */
	#progressbar div.progress{
		position:absolute;
		width:0;
		height:100%;
		overflow:hidden;
		background-color:#eef;
	}
	/* text on bar */
	#progressbar div.progress .text{
		position:absolute;
		text-align:center;
		color:black;
		font-size:45pt;
	}
	/* text off bar */
	#progressbar div.text{
		position:absolute;
		width:100%;
		height:100%;
		text-align:center;
		color:white;
		font-size:45pt;
	}
  </style>


<script type="text/javascript">
	var pct=0;
	var handle=0;
	function update(){
			$("#progressbar").reportprogress(pct);
			/*if(pct==100){
				clearInterval(handle);
				//$("#run").val("start");
				pct=0;
			}*/
	}
	/*jQuery(function($){
		$("#run").click(
			function(){
				if(this.value=="start"){
					handle=setInterval("update()",100);
					this.value="stop";
				}else{
					clearInterval(handle);
					this.value="start";
				}
			}
		);
		$("#reset").click(function(){
			pct=0;
			$("#progressbar").reportprogress(0);
		});
	});*/
</script>  
</head>
<body>
<div id="progressbar" style="display:none"></div>
<table width="100%">
<tr><td align="center">
<!--td></tr-->
<!--input type='button' id='run' value='start'/>
<input type='button' id='reset' value='reset'/-->
<!--tr><td align="center"-->
<div id="main"><div class="section">
<div class="demoInitial"><span id="waiting">Please wait ...<!--img src="images/circle-ball-dark-antialiased.gif" /></span--></div>
<div class="demoStatus"><span id="status">...</span></div>
<div class="demoSuccess"><?php //echo "Completed ";?><span id="completed"></span><?php echo "/";//echo " out of ";?><span id="total"></span></div>
<div class="demoWarning1"><?php //echo "Not Found ";?><span id="notfound">-</span></div>
<div class="demoWarning2"><?php //echo "Unknown ";?><span id="unknown">-</span></div>
</div></div>
</td></tr>
</table>
</body>
